package com.gxwy.upanyonghu.controller;

import java.util.Map;

import javax.servlet.http.HttpSession;


import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import com.gxwy.upanyonghu.form.UpanYongHu;
import com.gxwy.upanyonghu.service.UpanYongHuService;



@Controller
@RequestMapping("UpanYongHu")
public class UpanYongHuController {


		@Autowired
		private UpanYongHuService contactService; 

		@RequestMapping("/listss")
		public String listContacts(Map<String, Object> map,HttpSession session) {

			map.put("contact", new UpanYongHu());
			session.getAttribute("user");
			System.out.println(session);
			map.put("contactListss", contactService.findAll());
	      			return "UpanYongHu";
		}

		@RequestMapping(value = "/addC", method = RequestMethod.POST)
		public String addContact(@ModelAttribute("contact")
		UpanYongHu contact, BindingResult result,HttpSession session) {
	        
			contactService.save(contact);

	        return "redirect:listss";
		
		}
		
		@RequestMapping(value = "/update", method = RequestMethod.POST)
		public String update(@ModelAttribute("contact")
		UpanYongHu contact, BindingResult result,String name,Map<String, Object> map)
		{
			if(name==""){
				 return "redirect:listss";
			}
			contactService.Update(contact,Integer.valueOf(name));
		
			 return "redirect:listss";

			
		}

		@RequestMapping(value = "/delete",method = RequestMethod.POST)
		public String deleteContact(String delet) {
			if(delet==""){
		    return "redirect:listss";
				
			}
			contactService.Delete(Integer.valueOf(delet));

			return  "redirect:listss";
		}

	}


